package com.goods.business.service.imp;

import com.goods.business.mapper.InStockInfoMapper;
import com.goods.business.mapper.InStockMapper;
import com.goods.business.mapper.ProductStockMapper;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.InStockService;
import com.goods.common.model.business.InStock;
import com.goods.common.model.business.InStockInfo;
import com.goods.common.model.business.ProductStock;
import com.goods.common.model.business.Supplier;
import com.goods.common.vo.business.InStockItemVO;
import com.goods.common.vo.business.InStockVO;
import com.goods.common.vo.business.InStockVoList;
import com.goods.common.vo.business.SupplierVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class InStockServiceImpl implements InStockService {
    @Autowired
    private InStockMapper inStockMapper;

    @Override
    public List<InStockVO> findInStockList(Integer status, Integer type, String inNum, String startTime, String endTime) {
        Map<Object, Object> map = new HashMap<>();
        map.put("status", status);
        map.put("type", type);
        map.put("inNum", inNum);
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        List<InStockVO> inStockVOList = inStockMapper.findInStockList(map);
        return inStockVOList;
    }

    //查询明细
    @Override
    public Map detail(Long id) {
        //查询三表有关信息
        List<InStockItemVO> inStockItemVOList = inStockMapper.selectStockItem(id);
        //查询来源地
        SupplierVO supplierVO = inStockMapper.selectSupplier(id);
       /* InStockDetailVO inStockDetailVO = new InStockDetailVO();
        inStockDetailVO.setItemVOS(inStockItemVOList);
        inStockDetailVO.setSupplierVO(supplierVO);*/
        Map<Object, Object> map = new HashMap<>();
        map.put("itemVOS", inStockItemVOList);
        map.put("total", inStockItemVOList.size());
        map.put("supplierVO", supplierVO);
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        map.put("pStatus", inStock.getStatus());
        return map;
    }

    @Autowired
    private InStockInfoMapper inStockInfomapper;
    @Autowired
    private SupplierMapper supplierMapper;

    @Override
    public void addIntoStock(InStockVO inStockVO) {

        InStock inStock = new InStock();
        String inNum = UUID.randomUUID().toString();
        StringUtils.replace(inNum, "-", "");

        Long id = null;
        //如何判断是新建还是用以前的
        if (StringUtils.isEmpty(inStockVO.getSupplierId())) {
            Supplier supplier = getSupplier(inStockVO);
            supplier.setId(null);
            supplier.setName(inStockVO.getName());
            supplier.setEmail(inStockVO.getEmail());
            supplier.setPhone(inStockVO.getPhone());
            supplier.setSort(inStockVO.getSort());
            supplier.setContact(inStockVO.getContact());
            supplier.setCreateTime(Calendar.getInstance().getTime());
            supplier.setModifiedTime(Calendar.getInstance().getTime());
            int insert = supplierMapper.insert(supplier);
            id = supplier.getId();
        }
        //入库表
        BeanUtils.copyProperties(inStockVO, inStock);
        inStock.setId(null);
        inStock.setInNum(inNum);
        inStock.setProductNumber(inStockVO.getProducts().size());
        inStock.setCreateTime(Calendar.getInstance().getTime());
        inStock.setModified(Calendar.getInstance().getTime());
        inStock.setStatus(2);
        inStock.setOperator("admin");
        inStock.setSupplierId(id);
        inStockMapper.insert(inStock);
        //入库表详情
        List<InStockVoList> products = inStockVO.getProducts();
        InStockInfo inStockInfo = new InStockInfo();
        for (InStockVoList product : products) {
            String uuid = UUID.randomUUID().toString();
            System.out.println(uuid);
            System.out.println(uuid.length());
            inStockInfo.setId(null);
            inStockInfo.setInNum(inStock.getInNum());
            inStockInfo.setProductNumber(inStock.getProductNumber());
            inStockInfo.setPNum(uuid);
            inStockInfo.setCreateTime(Calendar.getInstance().getTime());
            inStockInfo.setModifiedTime(Calendar.getInstance().getTime());
            inStockInfomapper.insert(inStockInfo);
            //添加profuct——stock
            ProductStock productStock = new ProductStock();
            productStock.setPNum(uuid);
            productStock.setStock(product.getProductNumber());
            productStockMapper.insert(productStock);
        }
    }

    @Override
    public void publish(Long id) {
        InStock inStock = new InStock();
        inStock.setId(id);
        inStock.setStatus(0);
        inStockMapper.updateByPrimaryKeySelective(inStock);
    }

    @Override
    public void delete(Long id) {
        inStockMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void back(Long id) {
        InStock inStock = new InStock();
        inStock.setId(id);
        inStock.setStatus(0);
        inStockMapper.updateByPrimaryKeySelective(inStock);
    }

    @Override
    public void remove(Long id) {
        InStock inStock = new InStock();
        inStock.setId(id);
        inStock.setStatus(1);
        inStockMapper.updateByPrimaryKeySelective(inStock);
    }

    private Supplier getSupplier(InStockVO inStockVO) {
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(inStockVO, supplier);
        return supplier;
    }

    @Autowired
    private ProductStockMapper productStockMapper;
}
